package com.moyun.controller.system;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.ICaptcha;
import com.moyun.model.common.Constants;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * <h3>moyun-base</h3>
 * <p>图形验证码控制层</p>
 *
 * @author : ybx
 * @date : 2021-09-15 15:49:57
 **/
@Api(tags = "Authentication(系统认证)")
@Controller
public class CaptchaController {

    @ApiOperation("获取图形验证码")
    @GetMapping("captcha.jpg")
    public void getCaptcha(HttpServletRequest rep, HttpServletResponse resp) {
        //HuTool定义图形验证码的长和宽,验证码的位数，干扰线的条数
        ICaptcha captcha = CaptchaUtil.createLineCaptcha(116, 36, 4, 10);
        //将验证码放入session
        rep.getSession().setAttribute(Constants.CAPTCHA, captcha.getCode());
        try {
            ServletOutputStream outputStream = resp.getOutputStream();
            captcha.write(outputStream);
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
